/*泛型：不包含数据基本类型，而是一个类对象*/
public class Array<E> {
    private E[] data;
    private int size;   /*数组中有效的元素*/

    /*构造函数*/
    public Array(int capacity) {
        data = (E[]) new Object[capacity];
        size = 0;
    }

    /*无参数的构造函数*/
    public Array() {
        this(10);
    }

    /*获取数组中的元素个数*/
    public int getSize() {
        return size;
    }

    /*获取数组的容量*/
    public int getCapacity() {
        return data.length;
    }

    /*判断数组是否为空*/
    public boolean isEmpty() {
        return size == 0;
    }

    /*向指定位置添加元素*/
    public void insert(int index, E e) {
        if (index < 0 || index > size)
            throw new IllegalArgumentException("Failed! Index is illegal");
        if (size == data.length)
            resize(2 * data.length);

        for (int i = size - 1; i >= index; i--) {
            /*后一位元素的值=前一位元素的值 进行覆盖*/
            data[i + 1] = data[i];
        }
        data[index] = e;
        size++;
    }

    public void push(E e) {
        insert(size, e);
    }

    public void unshift(E e) {
        insert(0, e);
    }

    /*获取index索引位置的元素*/
    public E get(int index) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("Failed! Array is full.");
        return data[index];
    }

    /*修改index索引位置的元素*/
    public void set(int index, E e) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("Failed! Array is full.");
        data[index] = e;
    }

    /*查找数组中是否有元素*/
    public boolean includes(E e) {
        for (int i = 0; i < size; i++) {
            /*值比较*/
            if (data[i].equals(e))
                return true;
        }
        return false;
    }

    public int findIndex(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e))
                return i;
        }
        return -1;
    }

    /*删除指定位置的元素, 返回删除的元素*/
    public E remove(int index) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("Failed! Index is illegal");
        E ret = data[index];
        for (int i = index + 1; i < size; i++)
            data[i - 1] = data[i];
        size--;
        /*垃圾回收*/
        data[size] = null;

        /*防止复杂度的震荡*/
        if (size == data.length / 4 && data.length != 0)
            resize(data.length / 2);
        return ret;
    }

    /*删除数组中第一个元素*/
    public E shift() {
        return remove(0);
    }

    /*删除数组中最后一个元素*/
    public E pop() {
        return remove(size - 1);
    }

    /*删除指定元素*/
    public void delete(E e) {
        int index = findIndex(e);
        if (index != -1)
            remove(index);
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append(String.format("Array: size = %d, capacity = %d\n", size, data.length));
        res.append('[');
        for (int i = 0; i < size; i++) {
            res.append(data[i]);
            if (i != size - 1)
                res.append(", ");
        }
        res.append(']');
        return res.toString();
    }

    /*动态数组*/
    private void resize(int newCapacity) {
        E[] newData = (E[]) new Object[newCapacity];
        for (int i = 0; i < size; i++)
            newData[i] = data[i];
        data = newData;
    }
}
